package mww.servlet.teacher;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mww.common.DaoException;
import mww.common.Page;
import mww.common.ServiceException;
import mww.javabean.entity.Teacher;
import mww.javabean.service.TeacherService;
import mww.javabean.service.TeacherServiceImpl;

public class CheckTeacherServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TeacherService service = new TeacherServiceImpl();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String teacherNo = request.getParameter("teacherNo");
		String teacherName = request.getParameter("teacherName");
		Integer professional = null;
		if (request.getParameter("professional") != null && !request.getParameter("professional").equals("")) {
			professional = new Integer(request.getParameter("professional"));
		}
		String phone = request.getParameter("phone");
		String subject = request.getParameter("subject");
		request.setAttribute("teacherNo", teacherNo);
		request.setAttribute("teacherName", teacherName);
		request.setAttribute("professional", professional);
		request.setAttribute("phone", phone);
		request.setAttribute("subject", subject);
		int curPage = 1;
		String temp = request.getParameter("curPage");
		if (temp != null) {
			curPage = Integer.parseInt(request.getParameter("curPage"));
		}
		try {
			//查询条件封装进Teacher类
			Teacher condition=new Teacher();
			condition.setTeacherNo(teacherNo);
			condition.setTeacherName(teacherName);
			condition.setProfessional(professional);
			condition.setPhone(phone);
			condition.setSubject(subject);
			Map map = service.findAllTeacherByMostCon(condition, curPage);
			ArrayList list = (ArrayList) map.get("list");
			Page pa = (Page) map.get("pa");
			request.setAttribute("curPage", pa.getCurPage());// 向显示页传递当前页页码
			request.setAttribute("pageCount", pa.getPageCount());// 向显示页传递总页数
			request.setAttribute("list", list);// 向显示页传递结果集
			request.getRequestDispatcher("/admin/teacherList.jsp").forward(request, response);
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}
}
